package com.gmlive.common.sample;

import android.app.Application;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.util.Log;

import androidx.multidex.MultiDex;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.gmlive.common.host.manager.HostUpdateManager;
import com.gmlive.common.imageloader.ImageLoader;
import com.gmlive.common.network.domain.DomainConfig;
import com.gmlive.common.network.domain.DomainManager;
import com.gmlive.common.network.domain.request.OnDomainRequestCallback;
import com.gmlive.common.sample.update.AppUpdateUtils;
import com.gmlive.svgaplayer.DefaultSVGALoaderFactory;
import com.gmlive.svgaplayer.IkCosler;
import com.meelive.ingkee.push.InkePushConfig;
import com.meelive.ingkee.push.InkePushManage;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import coil.Coil;
import okhttp3.OkHttpClient;

/**
 * Create by: fanfeiyue
 * on: 2019-09-10
 * desc:
 */
public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        MultiDex.install(this);

        Fresco.initialize(this);

        Coil.setImageLoader(new DefaultImageLoaderFactory(this));

        IkCosler.setSvgaLoader(new DefaultSVGALoaderFactory(this));

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
            // push channel
            NotificationChannel channelPush = new NotificationChannel("GM_SDK_PUSH_CHANNEL", "GM SDK推送服务", NotificationManager.IMPORTANCE_HIGH);
            channelPush.setDescription("GM SDK推送服务, 请勿关闭, 以防通知栏服务不正常");
            channelPush.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            notificationManager.createNotificationChannel(channelPush);
        }

        //vivo需应用上架才能使用推送服务，在此不在演示
        //可通过log拿到registerId在控制台进行推送
        InkePushManage.setDebugMode(true);
        InkePushConfig config = new InkePushConfig.Builder(this)
                .enableJPush()
                .enableHWPush()
                .enableMiPush()
                .enableOppoPush()
                .build();
        InkePushManage.init(this, config);

        HostUpdateManager.ins().getBuilder()
                .setContext(this)
                .setIps("https://testservice.xiangshengclub.com/")
                .setUrl("/api/appconfig/get_raw")
                .setKey("key_ypcg_all")
                .setTest(true)
                .setThreadPoolExecutor((ThreadPoolExecutor) Executors.newFixedThreadPool(5))
                .build();

        ImageLoader.INSTANCE.init(this, "https://imagescalekyik.hnyapu.cn/imageproxy2/dimgm/scaleImage?");

        DomainConfig domainConfig = new DomainConfig.Builder()
                .addParam("key", "ssswitch_test_SLB")
                .setApiPath("/api/appconfig/get_raw")
                .setDomains("http://192.168.112.203", "http://192.144.195.173", "http://192.144.195.210")
                //.setOkHttpClient(createNoBusinessOkHttpClient())
                .setRequestCallback(new OnDomainRequestCallback() {
                    @Override
                    public void onRequestEnd(String ip, int code, long spendTime, String message) {
                        Log.i("DomainManager", "onRequestEnd: ip = " + ip + ", spend = " + spendTime + "ms" + ", code = " + code);
                    }
                })
                .setDevelopEnv(true)
                .build();

        DomainManager.getInstance().init(this, domainConfig);
        Log.i("DomainManager", "is domain available = " + DomainManager.getInstance().domainAvailable());

        AppUpdateUtils.INSTANCE.initUpdateSdk(this);
    }

    /**
     * 创建业务无关的OkHttpClient，Fresco等
     */
    private OkHttpClient createNoBusinessOkHttpClient() {
        return new OkHttpClient.Builder()
                .sslSocketFactory(SSLFactory.sslContext.getSocketFactory(), SSLFactory.xtm)
                .hostnameVerifier(SSLFactory.DO_NOT_VERIFY)
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .writeTimeout(20, TimeUnit.SECONDS)
                .build();
    }

}
